package com.jin.webvideo.externalInterfaces.video;

import com.jin.webvideo.entity.VideoInfo;
import com.jin.webvideo.entity.Video;
import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.mapper.VideoMapper;
import com.jin.webvideo.util.entity.EntityUtil;
import com.jin.webvideo.util.base.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LocalApi extends AbstractVideoAPI{
    @Autowired
    VideoMapper videoMapper;

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        Video video = videoMapper.findVideoByDocid(docid, PlatformSupport.PLATFORM_LOCAL);
        return EntityUtil.toFilmography(video);
    }

    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) {
        List<VideoInfo> infos = videoMapper.findVideoInfoByDocid(docid, PlatformSupport.PLATFORM_LOCAL);
        List<FilmographyInfo> list = EntityUtil.episode2VideoInfo(infos);
        list.stream().forEach(accept->{
            int mode = StringUtil.isEmpty(accept.getTitle())? FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK: FilmographyInfo.DISPLAY_MODE_TITLE_BLOCK;
            accept.setDisplayMode(mode);
        });
        list.sort( (o1, o2) -> o1.getNumber() - o2.getNumber() );
        return list;
    }

    public Filmography baseInfoFromDb(String docid, String tvid,String platform) throws UnavailableServiceException {
        Video video = videoMapper.findVideoByDocid(docid, platform);
        List<VideoInfo> infos = videoMapper.findVideoInfoByDocid(docid, platform);
        List<FilmographyInfo> list = EntityUtil.episode2VideoInfo(infos);
        list.stream().forEach(accept->{
            int mode = StringUtil.isEmpty(accept.getTitle())? FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK: FilmographyInfo.DISPLAY_MODE_TITLE_BLOCK;
            accept.setDisplayMode(mode);
        });
        list.sort( (o1, o2) -> o1.getNumber() - o2.getNumber() );
        Filmography filmography = EntityUtil.toFilmography(video);
        filmography.setVideoInfos(list);
        return filmography;
    }

    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_LOCAL;
    }
}
